package com.offcn.service;

import com.offcn.dao.CourseDao;
import com.offcn.dao.CourseDaoImpl;
import com.offcn.entity.Course;
import com.offcn.utils.PageUtils;

import java.io.File;
import java.util.List;
/**
 * 课程service层接口实现类
 */
public class CourseServiceImpl implements CourseService {
    private CourseDao courseDao = new CourseDaoImpl();


    @Override
    public PageUtils<Course> findByPage(int currentPage, int pageSize, String search) {
        int count = courseDao.findByPage("select * from course where courseName like ?", "%"+search+"%").size();
        List<Course> userList = courseDao.findByPage("select * from course where courseName like ? limit ?,?", "%" + search + "%", (currentPage - 1) * pageSize, pageSize);
        PageUtils<Course> pageUtils = new PageUtils<>(pageSize,currentPage,count,userList);
        return pageUtils;
    }

    @Override
    public boolean delAll(String[] cids) {
        try {
            for (String cid : cids) {
                int all = courseDao.delAll(cid);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        return true;
    }

    @Override
    public boolean addCourse(Course course) {
        return courseDao.addCourse(course)>0;
    }

    @Override
    public boolean amendCourse(Course course,String courseImage,String courseVideo) {
        if(!course.getCourseImage().equals(courseImage)){
            //如果图片有修改就执行文件删除操作
            File file1 = new File("E:\\Users\\tan\\Desktop\\upload",courseImage);
            file1.delete();
        }
        if(!course.getCourseVideo().equals(courseVideo)){
            //如果图片有修改就执行文件删除操作
            File file2 = new File("E:\\Users\\tan\\Desktop\\upload",courseVideo);
            file2.delete();
        }
        return courseDao.amendCourse(course)>0;
    }

    @Override
    public List<Course> findAllCourse() {
        return courseDao.findAllCourse();
    }
}
